/*******************************************************************************
*  file    : main.cpp
*  created : 20.07.2013
*  author  : Slyshyk Oleksiy (alex312@meta.ua)
*******************************************************************************/

#include "smsmainwindow.hpp"
#include <QApplication>
#include <QSharedPointer>
#include <QTextCodec>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QDebug>

#include <qlogger.hpp>
namespace {
QLogger* l = QLoggerEngine::getLogger("core");
QLogger* l_rpc = QLoggerEngine::getLogger("RPC");
QLogger* l_map = QLoggerEngine::getLogger("map");
int app_res = 0;
}

int main(int argc, char *argv[])
{
    try
        {
            QDir dir(QStringLiteral("../libqt/plugins"));
            QStringList dirs;
            dirs << QCoreApplication::libraryPaths();
            dirs << dir.absolutePath();
            dir.setPath(QStringLiteral("../libq5/plugins"));
            dirs << dir.absolutePath();
            QCoreApplication::setLibraryPaths(dirs);

            QApplication::setApplicationName   (QLatin1String("Sleeper"              ));
            QApplication::setOrganizationName  (QLatin1String("Slyshyk 0."           ));
            QApplication::setOrganizationDomain(QLatin1String("alexSlyshyk@gmail.com"));

            QApplication a(argc, argv);

#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
            QTextCodec::setCodecForTr    (QTextCodec::codecForName("UTF-8"));
#endif
#ifdef Q_OS_WIN
            QTextCodec::setCodecForLocale(QTextCodec::codecForName("CP-866"));
#endif

            QLoggerConsoleAppender* con_apdr =
                new QLoggerConsoleAppender(LevelError|LevelFatal|LevelCritical|LevelException,
                                           stdout,"%d\t<%l>\t{%c}\t%m[%f:%i]%n", "hh:mm:ss");
            QLoggerRollingFileAppender* rol_apdr =
                new QLoggerRollingFileAppender(LevelError|LevelFatal|LevelCritical|LevelException,
                                               "log/molesms.log","%d\t<%l>\t{%c}\t%m[%f:%i]%n", 1024*1024*5);
            l->addAppender(con_apdr);
            l->addAppender(rol_apdr);
            l_rpc->addAppender(con_apdr);
            l_rpc->addAppender(rol_apdr);
            l_map->addAppender(con_apdr);
            l_map->addAppender(rol_apdr);

            con_apdr =
                new QLoggerConsoleAppender(LevelDebug | LevelWarn | LevelInfo,
                                           stdout,"%d\t<%l>\t{%c}\t%m%n", "hh:mm:ss");
            rol_apdr =
                new QLoggerRollingFileAppender(LevelDebug | LevelWarn | LevelInfo,
                                               "log/molesms.log","%d\t<%l>\t{%c}\t%m%n", 1024*1024*5);
            l->addAppender(con_apdr);
            l->addAppender(rol_apdr);
            l_rpc->addAppender(con_apdr);
            l_rpc->addAppender(rol_apdr);
            l_map->addAppender(con_apdr);
            l_map->addAppender(rol_apdr);

            QSharedPointer<smsMainWindow> w = QSharedPointer<smsMainWindow>(new smsMainWindow);
            w->show();

            app_res = a.exec();
        }
    catch(...)
        {
            logCritical(l)<<QStringLiteral("Unexpected exception occur.");
        }

    return app_res;
}
